package org.example.classtopia.Service.Impl;

import org.example.classtopia.Model.Entity.Comment;
import org.example.classtopia.Mapper.CommentMapper;
import org.example.classtopia.Service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    private final CommentMapper commentMapper;

    @Autowired
    public CommentServiceImpl(CommentMapper commentMapper) {
        this.commentMapper = commentMapper;
    }

    @Override
    public int createComment(Comment comment) {
        // 设置默认值
        comment.setStatus(0); // 默认状态为正常
        comment.setCreateTime(LocalDateTime.now());
        comment.setLikeCount(0);
        return commentMapper.insertComment(comment);
    }

    @Override
    public Comment getCommentById(Long id) {
        return commentMapper.selectById(id);
    }

    @Override
    public List<Comment> getCommentsByPostId(Long postId, int page, int size) {
        int offset = page * size;
        return commentMapper.selectByPostIdWithPaging(postId, offset, size);
    }

    @Override
    public int updateCommentStatus(Long id, Integer status) {
        return commentMapper.updateStatus(id, status);
    }
}

